<template>
  <div class="register-container">
    <div class="register-box">
      <h2>注册账号</h2>
      <el-form :model="form" :rules="rules" ref="registerForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="确认密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="form.email" placeholder="邮箱" prefix-icon="Message" />
        </el-form-item>
        <div class="form-actions">
          <el-button type="primary" :style="{backgroundColor: '#4f46e5'}" @click="handleRegister" class="register-btn">注册</el-button>
        </div>
        <div class="form-links">
          <router-link to="/login" class="link">已有账号？立即登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>


<script>
import fetch  from '@/api/fetch'

export default {
  name: 'UserRegister',
    data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.form.confirmPassword !== '') {
          this.$refs.registerForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        confirmPassword: '',
        email: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
        confirmPassword: [{ validator: validatePass2, trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleRegister() {
      this.$refs.registerForm.validate(async valid => {
        if (valid) {
          try {
            const userData = {
              username: this.form.username,
              passwordHash: this.form.password,
              email: this.form.email
            }
            fetch('/client-user/user/pc/register', userData, {method: 'post'}).then((res) => {
              console.log(res)
              if (res) {
                if (res.code === '200' && res.result) {
                  this.$message.success('注册成功')
                  this.$router.push('/login')
                }
              }
            })
          } catch (error) {
            this.$message.error(error.response?.data?.message || '注册失败，请重试')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f3f4f6;
  padding: 1rem;
}

.register-box {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: #4f46e5;
  margin-bottom: 2rem;
  font-size: clamp(1.5rem, 4vw, 2rem);
}

.form-actions {
  margin-top: 1.5rem;
}

.register-btn {
  width: 100%;
  padding: 0.75rem;
  font-size: clamp(0.875rem, 2vw, 1rem);
}

.form-links {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}

.link {
  color: #4f46e5;
  text-decoration: none;
  font-size: clamp(0.75rem, 2vw, 0.875rem);
}

.link:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .register-box {
    padding: 1.5rem;
  }

  .form-links {
    justify-content: center;
  }

  .link {
    margin: 0 0.5rem;
  }
}
</style>